package com.sleep.god.common.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.util.Auth;

import java.io.IOException;

public class QiniuUtils {

    //设置好账号的ACCESS_KEY和SECRET_KEY
    public static String ACCESS_KEY = "8mAy18ftJZ0Napa1N2WJJ4y4n6dZcqpLR7dEIEca"; //这两个登录七牛 账号里面可以找到
    public static String SECRET_KEY = "T20ujfKxVjVgyGSrME10DD1ROzejAiFQ3tkHKfu0";

    //要上传的空间
    String bucketname = "elecinspection"; //对应要上传到七牛上 你的那个路径（自己建文件夹 注意设置公开）

    //密钥配置
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    //创建上传对象
    UploadManager uploadManager = new UploadManager(cfg);

    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public String getUpToken() {
        return auth.uploadToken(bucketname);
    }

    //普通上传
    public Response upload(String filePath, String key) throws IOException {
        try {
            //调用put方法上传
            Response res = uploadManager.put(filePath, key, getUpToken());
            //打印返回的信息
            System.out.println(res.bodyString());
            return res;
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
            return r;
        }
    }

    public static void main(String[] args) {
        //String url = "http://owbclyi53.bkt.clouddn.com/201807131157122.xlsx";
        String[] keys = new String[]{
                "201807131157122.xlsx",
                "201807131146351.xlsx"
        };
        QiniuUtils qiniuUtil = new QiniuUtils();
        //qiniuUtil.delete(url);
        qiniuUtil.downloadBatch(keys);
    }

    public void delete(String key) {
        //构造一个带指定Zone对象的配置类
        //Configuration cfg = new Configuration(Zone.zone0());

        //...其他参数参考类注释
        //        String accessKey = "your access key";
        //        String secretKey = "your secret key";
        //        String bucket = "your bucket name";
        //        String key = "your file key";
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucketname, key);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }

    public void downloadBatch(String[] keyList) {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
//        String accessKey = "your access key";
//        String secretKey = "your secret key";
//        String bucket = "your bucket name";
//        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            //单次批量请求的文件数量不得超过1000
            //String[] keyList = new String[]{
            //        "qiniu.jpg",
            //        "qiniu.mp4",
            //        "qiniu.png",
            //};
            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
            batchOperations.addStatOps(bucketname, keyList);
            Response response = bucketManager.batch(batchOperations);
            BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
            for (int i = 0; i < keyList.length; i++) {
                BatchStatus status = batchStatusList[i];
                String key = keyList[i];
                System.out.print(key + "\t");
                if (status.code == 200) {
                    //文件存在
                    System.out.println(status.data.hash);
                    System.out.println(status.data.mimeType);
                    System.out.println(status.data.fsize);
                    System.out.println(status.data.putTime);
                } else {
                    System.out.println(status.data.error);
                }
            }
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
    }

    public void deleteBatch(String[] keyList) {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
//        String accessKey = "your access key";
//        String secretKey = "your secret key";
//        String bucket = "your bucket name";
//        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            //单次批量请求的文件数量不得超过1000
            //String[] keyList = new String[]{
            //        "qiniu.jpg",
            //        "qiniu.mp4",
            //        "qiniu.png",
            //};
            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
            batchOperations.addDeleteOp(bucketname, keyList);
            Response response = bucketManager.batch(batchOperations);
            BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
            for (int i = 0; i < keyList.length; i++) {
                BatchStatus status = batchStatusList[i];
                String key = keyList[i];
                System.out.print(key + "\t");
                if (status.code == 200) {
                    System.out.println("delete success");
                } else {
                    System.out.println(status.data.error);
                }
            }
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
    }
}